
const { createLeave, queryAll, adminQueryAll, updateById, removeById } = require('../service/leave.service')

class LeaveController {

    async create(ctx) {
        const emp = ctx.state.emp
        const { remark, dayNum } = ctx.request.body
        const leave = {
            'empId': emp.id,
            'empName': emp.empName,
            remark,
            'initiationTime': Date.now(),
            dayNum,
            'status': 0
        }
        const res = await createLeave(leave)
        ctx.body = {
            code: '200',
            message: '申请已发起，待审核',
            data: res
        }
    }

    async update(ctx) {
        const id = ctx.request.params.id
        const { remark, dayNum } = ctx.request.body
        const initiationTime = Date.now()
        const leave = {
            id,
            remark,
            initiationTime,
            dayNum,
        }
        await updateById(leave)
        ctx.body = {
            code: '200',
            message: '修改成功'
        }
    }

    async query(ctx) {
        const whereOpt = { empId: ctx.state.emp.id }
        const res = await queryAll(whereOpt)
        ctx.body = {
            code: '200',
            message: '查询成功',
            data: res
        }
    }

    async statusQuery(ctx){
        const whereOpt = { empId: ctx.state.emp.id,status:ctx.request.params.status }
        console.log(whereOpt);
        const res = await queryAll(whereOpt)
        ctx.body = {
            code: '200',
            message: '查询成功',
            data: res
        }
    }

    async adminQuery(ctx) {
        const { pageNum = 1, pageSize = 5 } = ctx.request.query
        const whereOpt = { status: 0 }
        const res = await adminQueryAll(pageNum, pageSize, whereOpt)
        ctx.body = {
            code: '200',
            message: '查询成功',
            data: res
        }
    }

    async auditQueryAll(ctx) {
        const { pageNum = 1, pageSize = 5 } = ctx.request.query
        const whereOpt = { reviewId: ctx.state.emp.id }
        const res = await adminQueryAll(pageNum, pageSize, whereOpt)
        ctx.body = {
            code: '200',
            message: '查询成功',
            data: res
        }
    }

    async auditStatusQueryAll(ctx){
        const { pageNum = 1, pageSize = 5 } = ctx.request.query
        const whereOpt = { reviewId: ctx.state.emp.id ,status:ctx.request.params.status}
        const res = await adminQueryAll(pageNum, pageSize, whereOpt)
        ctx.body = {
            code: '200',
            message: '查询成功',
            data: res
        }
    }

    async audit(ctx) {
        const id = ctx.request.params.id
        const status = ctx.request.params.status
        console.log(id, status);
        const reviewId = ctx.state.emp.id
        const reviewName = ctx.state.emp.empName
        updateById({ id, status, reviewId, reviewName })
        ctx.body = {
            code: '200',
            message: '审核完成'
        }
    }

    async remove(ctx) {
        const id = ctx.request.params.id
        await removeById(id)
        ctx.body = {
            code: '200',
            message: '删除成功'
        }
    }
}

module.exports = new LeaveController()